﻿/*
Copyright (c) 2013, Douglas Stebila, Brendon Park, David Sweeney, Nathan Wills, Simon Lockhart, Thomas Phan
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Security.Cryptography;

namespace NMLE.HCE
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="THashAlgorithm">
    ///     The Algorythm to he used for key derivation
    /// </typeparam>
    /// <typeparam name="TEncryptionAlgorithm">
    ///     The Algorythm used for the cryptographic operations
    /// </typeparam>
    public class HCETransform<THashAlgorithm, TEncryptionAlgorithm>
        : MLETransformBase<THashAlgorithm,TEncryptionAlgorithm>
        where THashAlgorithm : HashAlgorithm, new()
        where TEncryptionAlgorithm : SymmetricAlgorithm, new()
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="transformKey"></param>
        /// <param name="direction">
        ///     The encryption direction to create the transform
        /// </param>
        public HCETransform(
            Byte[] transformKey,
            EncryptionDirection direction
            ) : base(transformKey,direction)
        {
        }

        /// <summary>
        ///     Transforms the specified region of the input byte array and
        ///     copies the resulting transform to the specified region of the
        ///     output byte array.
        /// </summary>
        /// <returns>
        ///     The number of bytes written.
        /// </returns>
        /// <param name="inputBuffer">
        ///     The input for which to compute the transform.
        /// </param>
        /// <param name="inputOffset">
        ///     The offset into the input byte array from which to begin using
        ///     data.
        /// </param>
        /// <param name="inputCount">
        ///     The number of bytes in the input byte array to use as data.
        /// </param>
        /// <param name="outputBuffer">
        ///     The output to which to write the transform.
        /// </param>
        /// <param name="outputOffset">
        ///     The offset into the output byte array from which to begin
        ///     writing data.
        /// </param>
        public override int TransformBlock(
            byte[] inputBuffer,
            int inputOffset,
            int inputCount,
            byte[] outputBuffer,
            int outputOffset
            )
        {
            var result = UnderlyingSymetricTransform.TransformBlock(
                inputBuffer,
                inputOffset,
                inputCount,
                outputBuffer,
                outputOffset
                );

            var hashBuffer = Direction == EncryptionDirection.Decrypt
                ? outputBuffer.Clone() as byte[]
                : inputBuffer.Clone() as byte[];

            UnderlyingHashTransform.TransformBlock(
                hashBuffer,
                inputOffset,
                inputCount,
                hashBuffer,
                0
                );

            return result;
        }

        /// <summary>
        ///     Transforms the specified region of the specified byte array.
        /// </summary>
        /// <returns>
        ///     The computed transform.
        /// </returns>
        /// <param name="inputBuffer">
        ///     The input for which to compute the transform.
        /// </param>
        /// <param name="inputOffset">
        ///     The offset into the byte array from which to begin using data.
        /// </param>
        /// <param name="inputCount">
        ///     The number of bytes in the byte array to use as data.
        /// </param>
        public override byte[] TransformFinalBlock(
            byte[] inputBuffer,
            int inputOffset,
            int inputCount
            )
        {

            var lastBytes = UnderlyingSymetricTransform.TransformFinalBlock(
                inputBuffer,
                inputOffset,
                inputCount
                );

            var hashBuffer = Direction == EncryptionDirection.Decrypt
                ? lastBytes.Clone() as byte[]
                : inputBuffer.Clone() as byte[];

            UnderlyingHashTransform.TransformFinalBlock(
                hashBuffer,
                inputOffset,
                inputCount
                );

           // if (Direction == EncryptionDirection.Encrypt)
           //     ReportHashCheck(UnderlyingHashTransform.Hash);

            return lastBytes;
        }

        public override byte[] Key
        {
            get { return TransformKey; }
        }
    }
}